#include "staticfunctions.h"
#include "pointdialog.h"

PointDialog::PointDialog(QWidget *parent) :
  QDialog(parent)
{
  ui.setupUi(this);

  m_currPoint = 0;
  m_directionVector = new DirectionVectorWidget(ui.directionbox);  
  m_directionVector->setRange(0, 1000, 1);
  connect(m_directionVector, SIGNAL(directionChanged(float, Vec)),
	  this, SLOT(directionChanged(float, Vec)));

  m_points.clear();
  m_row = -1;
  m_column = -1;

  connect(&m_delegate, SIGNAL(valueChanged(double)),
	  this, SLOT(valueChanged(double)));

  connect(ui.tableView, SIGNAL(doubleClicked(QModelIndex)),
	  this, SLOT(doubleClicked(QModelIndex)));

  m_model.setColumnCount(3);
  QStringList list;
  list << "x" << "y" << "z";
  m_model.setHorizontalHeaderLabels(list);

  ui.tableView->setModel(&m_model);
  ui.tableView->setItemDelegate(&m_delegate);

  clear();
}
PointDialog::~PointDialog()
{
  clear();
}

void
PointDialog::setHideRawValues(bool flag)
{
  ui.showvalues->setVisible(!flag);
}

void
PointDialog::clear()
{
  m_model.setRowCount(0);
  m_points.clear();
  ui.tabWidget->hide();
  ui.showpoints->hide();
  ui.showvalues->hide();
}

void
PointDialog::setPoints(QList<Vec> pts)
{
  m_points = pts;

  if (m_points.count() > 0)
    {
      ui.tabWidget->show();
      ui.showpoints->show();
      ui.showvalues->show();
    }
  else
    {
      clear();
      return;
    }

  disconnect(&m_model, 0, this, 0);

  m_model.setRowCount(pts.count());

  QStringList list;
  for(int i=0; i<pts.count(); i++)
    list << QString("%1").arg(i);

  ui.pointlist->clear();
  ui.pointlist->addItems(list);

  m_model.setVerticalHeaderLabels(list);
  
  
  for(int i=0; i<pts.count(); i++)
    {
      QModelIndex index;
      float x = m_points[i].x;
      float y = m_points[i].y;
      float z = m_points[i].z;

      // this is done to keep two digits after decimal.
      x = (int)x + (int)((x-(int)x)*100)*0.01;
      y = (int)y + (int)((y-(int)y)*100)*0.01;
      z = (int)z + (int)((z-(int)z)*100)*0.01;


      index = m_model.index(i, 0, QModelIndex());
      m_model.setData(index, QVariant(x));

      index = m_model.index(i, 1, QModelIndex());
      m_model.setData(index, QVariant(y));

      index = m_model.index(i, 2, QModelIndex());
      m_model.setData(index, QVariant(z));
    }  

  m_row = -1;
  m_column = -1;
}

void PointDialog::valueChanged(double d)
{
  if (m_row < 0 || m_column < 0)
    return;

  m_points[m_row][m_column] = d;

  emit pointChanged(m_row, m_points[m_row]);
}

void
PointDialog::changePoints()
{
  QList<Vec> points;
  for(int i=0; i<m_model.rowCount(); i++)
    {
      QModelIndex index;
      float x,y,z;

      index = m_model.index(i, 0, QModelIndex());
      x = m_model.data(index).toDouble();
      index = m_model.index(i, 1, QModelIndex());
      y = m_model.data(index).toDouble();
      index = m_model.index(i, 2, QModelIndex());
      z = m_model.data(index).toDouble();

      points.append(Vec(x,y,z));
    }

  emit pointsChanged(points);
}

void
PointDialog::on_showpoints_clicked(bool flag)
{
  if (flag)
    emit showPoints(true);
  else
    emit showPoints(false);
}

void
PointDialog::on_showvalues_clicked(bool flag)
{
  if (flag)
    emit showRawValues(true);
  else
    emit showRawValues(false);
}

void
PointDialog::doubleClicked(QModelIndex idx)
{
  m_row = idx.row();
  m_column = idx.column();

  emit pointNumber(m_row);
}

void
PointDialog::on_pointlist_itemClicked(QListWidgetItem *item)
{
  m_currPoint = ui.pointlist->row(item);

  Vec v = m_points[m_currPoint] + m_directionVector->vector();  

  emit pointNumber(m_currPoint);
  emit directionChanged(m_currPoint, v);
}

void
PointDialog::directionChanged(float len, Vec dv)
{  
  Vec v = m_points[m_currPoint] + len*dv;
  emit directionChanged(m_currPoint, v);
}

void
PointDialog::on_apply_pressed()
{
  Vec v = m_points[m_currPoint] + m_directionVector->vector();  
  emit pointChanged(m_currPoint, v);

  m_points[m_currPoint] = v;
  QModelIndex index;
  index = m_model.index(m_currPoint, 0, QModelIndex());
  m_model.setData(index, QVariant(v.x));

  index = m_model.index(m_currPoint, 1, QModelIndex());
  m_model.setData(index, QVariant(v.y));

  index = m_model.index(m_currPoint, 2, QModelIndex());
  m_model.setData(index, QVariant(v.z));
}

void
PointDialog::on_tabWidget_currentChanged(int idx)
{
  if (idx == 0)
    {
      Vec v = Vec(0,0,0);
      emit directionChanged(-1, v);
    }
  else
    {
      if (m_currPoint >= 0)
	{
	  Vec v = m_points[m_currPoint] + m_directionVector->vector();  
	  emit directionChanged(m_currPoint, v);
	}
    }
}
